Are you a Java developer looking to set up your development environment on a Linux machine? If so, you’ve come to the right place. In this comprehensive guide, we’ll walk you through the process of setting up and installing Intellij, one of the most popular integrated development environments (IDEs) for Java, along with the Java Development Kit (JDK) on your Linux system.
What is Intellij?
Intellij is an IDE developed by JetBrains, designed specifically for Java development. It offers a wide range of features and tools that make coding, debugging, and testing Java applications much easier and more efficient. Some of the key features of Intellij include:
- Smart code completion
- On-the-fly code analysis
- Refactoring tools
- Built-in version control integration
- Debugging and testing tools
Intellij Editions
Intellij comes in two editions: Community and Ultimate. The Community edition is free and open-source, while the Ultimate edition is a paid version with additional features and support for web and enterprise development.
Community Edition Features
- Java, Kotlin, Groovy, and Scala support
- Version control integration (Git, SVN, Mercurial)
- Build tools (Maven, Gradle)
- Testing tools (JUnit, TestNG)
- Debugging tools
Ultimate Edition Features
- All Community edition features
- Web development support (HTML, CSS, JavaScript)
- Framework support (Spring, Java EE, Play, Grails)
- Database tools and SQL support
- Advanced testing and profiling tools
What is the Java Development Kit (JDK)?
The Java Development Kit (JDK) is a software development kit that provides the necessary tools and libraries for developing Java applications. It includes the Java Runtime Environment (JRE), which is required to run Java programs, as well as the Java compiler, which is used to compile Java source code into bytecode.
JDK Components
The JDK consists of several components, including:
- Java Compiler (javac): Compiles Java source code into bytecode
- Java Virtual Machine (JVM): Executes Java bytecode
- Java Class Libraries: Provide pre-written code for common programming tasks
- Java Documentation Generator (javadoc): Creates documentation from source code comments
- Java Debugger (jdb): Helps find and fix bugs in Java programs
JDK Versions
Oracle releases new versions of the JDK on a regular basis. As of 2021, the latest long-term support (LTS) version is JDK 11, while the latest feature release is JDK 16. It’s important to choose the JDK version that is compatible with your project’s requirements and the version of Intellij you plan to use.
Prerequisites
Before we dive into the installation process, make sure your Linux system meets the following requirements:
- A 64-bit Linux distribution (Ubuntu, Debian, Fedora, etc.)
- At least 4 GB of RAM
- At least 2 GB of free disk space
- A processor with a minimum speed of 2 GHz
Checking Your System Architecture
To check if your Linux system is 64-bit, open a terminal and run the following command:
uname -m
If the output is “x86_64”, your system is 64-bit.
Checking Your RAM
To check the amount of RAM on your system, run the following command in the terminal:
free -h
Look for the “Mem:” line in the output to see your total RAM.
Checking Your Disk Space
To check the available disk space on your system, use the following command:
df -h
Look for the root filesystem (“/”) in the output to see the available space.
Step 1: Download and Install the JDK
The first step in setting up Intellij is to download and install the JDK. Follow these steps:
- Open a web browser and go to the official Oracle JDK download page: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html
- Accept the license agreement and download the appropriate JDK package for your Linux distribution. For example, if you’re using Ubuntu, download the “.deb” package.
- Once the download is complete, open a terminal and navigate to the directory where you saved the JDK package.
- Run the following command to install the JDK:
sudo apt install ./jdk-11.0.11_linux-x64_bin.deb
Replace “jdk-11.0.11_linux-x64_bin.deb” with the actual filename of the JDK package you downloaded.
- Wait for the installation process to complete.
Verifying the JDK Installation
To verify that the JDK was installed correctly, run the following command in the terminal:
java -version
You should see output similar to the following:
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
Setting the JAVA_HOME Environment Variable
Many Java applications and tools, including Intellij, rely on the JAVA_HOME
environment variable to locate the installed JDK. To set this variable, follow these steps:
- Open a terminal and run the following command to determine the path to your JDK installation:
sudo update-alternatives --config java
- Look for the path to your JDK installation in the output. It should look something like “/usr/lib/jvm/java-11-openjdk-amd64/bin/java”.
- Open the “/etc/environment” file in a text editor with root privileges:
sudo nano /etc/environment
- Add the following line to the file, replacing “/usr/lib/jvm/java-11-openjdk-amd64” with the path to your JDK installation:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
- Save the file and exit the text editor.
- Reload the environment variables:
source /etc/environment
- Verify that the
JAVA_HOME
variable is set correctly:
echo $JAVA_HOME
You should see the path to your JDK installation.
Step 2: Download and Install Intellij
Now that you have the JDK installed, you can proceed with downloading and installing Intellij. Here’s how:
- Go to the official Intellij download page: https://www.jetbrains.com/idea/download/#section=linux
- Choose the edition you want to download (Community or Ultimate) and click the “Download” button.
- Once the download is complete, open a terminal and navigate to the directory where you saved the Intellij package.
- Extract the package using the following command:
tar -xzf ideaIC-2021.1.1.tar.gz
Replace “ideaIC-2021.1.1.tar.gz” with the actual filename of the Intellij package you downloaded.
- Move the extracted directory to the “/opt” directory:
sudo mv idea-IC-211.7142.45 /opt/idea
Replace “idea-IC-211.7142.45” with the actual directory name.
- Create a symlink to the Intellij executable:
sudo ln -s /opt/idea/bin/idea.sh /usr/local/bin/idea
Running Intellij
To run Intellij, simply type the following command in the terminal:
idea
Intellij should launch, and you’ll be greeted with the welcome screen.
Configuring Intellij for First Use
When you launch Intellij for the first time, you’ll need to complete a few initial configuration steps:
- Choose your UI theme (Light or Darcula) and click “Next”.
- Choose your preferred keymap (e.g., Eclipse, NetBeans, Visual Studio) and click “Next”.
- Select the plugins you want to install and click “Next”. You can also install plugins later from within Intellij.
- Click “Start using Intellij IDEA” to complete the initial configuration.
Step 3: Configuring Intellij
Now that you have Intellij up and running, it’s time to configure it for your Java development needs.
Setting Up the JDK
- In Intellij, go to “File” > “Project Structure”.
- In the “Project” tab, under “Project SDK”, click “New” and select “JDK”.
- Navigate to the directory where you installed the JDK (e.g., “/usr/lib/jvm/java-11-openjdk-amd64”) and click “OK”.
- Click “Apply” and then “OK” to close the Project Structure dialog.
Configuring the Compiler
- In Intellij, go to “File” > “Settings” (or “Intellij IDEA” > “Preferences” on macOS).
- In the left-hand pane, navigate to “Build, Execution, Deployment” > “Compiler”.
- Under “Java Compiler”, ensure that the “Use compiler” option is selected and the correct JDK version is specified.
- Click “Apply” and then “OK” to close the Settings dialog.
Configuring the Code Style
Intellij comes with a set of predefined code style settings, but you can customize these to suit your preferences:
- Go to “File” > “Settings” (or “Intellij IDEA” > “Preferences” on macOS).
- In the left-hand pane, navigate to “Editor” > “Code Style”.
- Choose the programming language you want to configure (e.g., Java) from the list at the top of the pane.
- Customize the code style settings to your liking. You can control things like indentation, spacing, wrapping, and more.
- Click “Apply” and then “OK” to close the Settings dialog.
Installing Plugins
Intellij has a vast ecosystem of plugins that can extend its functionality and improve your development workflow. To install plugins:
- Go to “File” > “Settings” (or “Intellij IDEA” > “Preferences” on macOS).
- In the left-hand pane, click “Plugins”.
- Search for the plugin you want to install in the search bar at the top of the pane.
- Click the “Install” button next to the plugin.
- Restart Intellij when prompted to activate the plugin.
Some popular plugins for Java development include:
- Lombok: Reduces boilerplate code
- SonarLint: Provides real-time feedback on code quality
- CheckStyle-IDEA: Enforces coding standards
- JRebel: Enables hot reloading of code changes
Step 4: Creating and Running a Java Project
Now that you have Intellij configured, let’s create and run a simple Java project.
Creating a New Java Project
- In Intellij, go to “File” > “New” > “Project”.
- Select “Java” from the list of project types and click “Next”.
- Give your project a name and choose a location to save it.
- If prompted, select the JDK you configured earlier as the project SDK.
- Click “Finish” to create the project.
Writing Java Code
- In your new project, right-click the “src” directory and go to “New” > “Java Class”.
- Give your class a name (e.g., “Main”) and click “OK”.
- Write your Java code in the new class file. For example:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Running Java Code
To run your Java code:
- Right-click anywhere in the class file and select “Run” followed by the name of your class (e.g., “Main”).
- Intellij will compile and run your code. The output will appear in the “Run” tool window at the bottom of the screen.
Debugging Java Code
Intellij provides a powerful debugger that allows you to step through your code, set breakpoints, inspect variables, and more. To debug your code:
- Set a breakpoint by clicking in the gutter next to the line of code where you want the debugger to pause.
- Right-click anywhere in the class file and select “Debug” followed by the name of your class.
- Intellij will compile your code and pause at the breakpoint.
- Use the debug controls at the top of the screen to step through your code, inspect variables, and more.
Step 5: Using Version Control
Version control is an essential tool for any software development project. Intellij has built-in support for popular version control systems like Git, SVN, and Mercurial.
Setting Up Git
To set up Git in Intellij:
- Go to “File” > “Settings” (or “Intellij IDEA” > “Preferences” on macOS).
- In the left-hand pane, navigate to “Version Control” > “Git”.
- Specify the path to your Git executable (e.g., “/usr/bin/git”).
- Click “Test” to verify that Intellij can communicate with Git.
- Click “OK” to close the Settings dialog.
Creating a Git Repository
To create a new Git repository for your project:
- Go to “VCS” > “Enable Version Control Integration”.
- Select “Git” from the list of version control systems and click “OK”.
- Intellij will create a new Git repository for your project.
Committing Changes
To commit changes to your Git repository:
- Go to “VCS” > “Commit”.
- Select the files you want to commit in the “Commit Changes” dialog.
- Enter a commit message describing your changes.
- Click “Commit” to commit your changes to the repository.
Pushing and Pulling Changes
To push your changes to a remote Git repository:
- Go to “VCS” > “Git” > “Push”.
- Specify the remote repository URL and branch.
- Click “Push” to push your changes to the remote repository.
To pull changes from a remote Git repository:
- Go to “VCS” > “Git” > “Pull”.
- Specify the remote repository URL and branch.
- Click “Pull” to pull changes from the remote repository.
Step 6: Building and Deploying Your Application
Once you’ve written your Java code, you’ll need to build and deploy your application.
Building with Maven
Maven is a popular build automation tool for Java projects. To use Maven in Intellij:
- Go to “File” > “New” > “Project”.
- Select “Maven” from the list of project types and click “Next”.
- Give your project a name and click “Finish”.
- Intellij will create a new Maven project with a default directory structure and a “pom.xml” file.
- Add your Java code to the “src/main/java” directory.
- To build your project, open the Maven tool window (View > Tool Windows > Maven) and run the “package” goal.
Building with Gradle
Gradle is another popular build automation tool for Java projects. To use Gradle in Intellij:
- Go to “File” > “New” > “Project”.
- Select “Gradle” from the list of project types and click “Next”.
- Give your project a name and click “Finish”.
- Intellij will create a new Gradle project with a default directory structure and a “build.gradle” file.
- Add your Java code to the “src/main/java” directory.
- To build your project, open the Gradle tool window (View > Tool Windows > Gradle) and run the “build” task.
Deploying to a Web Server
To deploy your Java web application to a web server like Tomcat or Jetty:
- Go to “Run” > “Edit Configurations”.
- Click the “+” button and select “Tomcat Server” or “Jetty Server” from the list of configuration types.
- Configure the server settings (e.g., server port, deployment directory).
- Under “Deployment”, click the “+” button and select “Artifact”.
- Select the artifact you want to deploy (e.g., a WAR file) and click “OK”.
- Click “Apply” and then “OK” to close the Run/Debug Configurations dialog.
- Go to “Run” > “Run” and select your server configuration to start the server and deploy your application.