Understanding Nexus: Developer Guide

Understanding Nexus: Developer Guide

In the fast-changing world of making computer programs, keeping things organized is super important for things to run smoothly. For that artifact(repository) managers were created. Repositry manager is used to store build files and it exposes API to push and pull build so we can easily integrate it with automation tools like Jenkins.

Nexus is one of the big players in helping with that. In this multi-part blog series, we’re going to talk about Nexus — what it is, and how it’s different from other artifact managers, and we’ll show you how to set it up on Linux and Docker containers, step by step.

What is Artificat?

In simple words just think of that as a by-product we get after running the build command. If you have worked on React Framework, you have run an npm run build command and then immediately get a build folder, That is an artifact. It can be any format it might be in zip format, .jar format, or any other format. It is just a folder we get after development that we can ship from server to server easily.

What is Nexus?

Nexus is an artifact manager, which simply means it helps us to save, retrieve artifacts, and make a backup of them if we want. Nexus is built by Sonatype and is a very popular artifact manager, it also exposes API endpoints which makes it easy for us to integrate it with an automation tool like Jenkins or GitHub Action. It also gives us a UI interface so we can interact with it easily.

Nexus vs. Other Artifact Managers

Now the question arises why Nexus? There are many other managers out there like npm, Maven Central Repository, and Dockerhub, These are all popular managers that are widely used. Then why do we need Nexus?

Now there are many reasons companies prefer to use Nexus I will try to list down a few of them :

  1. If you are working in a big company where there are many types of programs you need to maintain a repository manager for each of them individually. You can not push the Node app into Dockerhub, nor you can push the Java app to npm. These managers serve only a single type and you can not save any other format there. But Nexus you can handle all sorts of programs: Node apps, Docker images, Helm Charts for Kubernetes, Java apps, and more. Check all supported format types from this like https://help.sonatype.com/repomanager3/nexus-repository-administration/formats.

  2. Many companies do not trust 3rd parties like npm or Dockerhub with their code. So they prefer to set up their own repository manager where they can share artifacts within the company that can not be accessible from outside.

  3. With Nexus you can set up a proxy repository as well with in built cache system. For example, one of the company’s software developer tried to install dependencies for his/her program. Now your Nexus manager does not have all the packages that were requested, so Nexus will make a request to the actual repository manager like npm or Dockehub whatever you set up through a proxy, and fetch all those packages and cache them. So next time you will get them directly from Nexus and it will save a lot of bandwidth.

Installing Nexus On Linux System (Ubuntu 22.04)

As a prerequisite, we need a Java OpenJDK and JRE v8. Lucky for us we don't need to go anywhere to install this, the default Ubuntu repository provides multiple Java versions, So now you will install the Java OpenJDK and JRE v8 from the Ubuntu repository.

First update and refresh repositories package indexes by running.

sudo apt update
sudo apt install openjdk-8-jdk

Now run the above command to install openjdk-8-jdk. Press Y when asked and continue with the installation.

After completing the installation check the Java Version.

java -version

You will get the result with your Java version if the installation was smooth, else you will get an error on this command.

Installing Nexus Repository Manager

Now we will download the Nexus binary from their official documentation.

https://help.sonatype.com/repomanager3/product-information/download

Now for this blog, I am using the 3.41 version but you can change it with the current latest version of your time, when I am writing this blog it is 3.62. We will use the wget utility to download that. After successful completion, you can see the binary on the folder where you run the command from.

wget https://download.sonatype.com/nexus/3/nexus-3.41.1-01-unix.tar.gz

untaring it.

tar xzf nexus-3.41.1-01-unix.tar.gz

It is a good practice to make a folder in the opt folder called Nexus and put the untar version there. Run the following command to do that.

mv nexus-3.41.1-01 /opt/nexus
mv sonatype-work /opt/

So on untaring that binary, we will actually get 2 folders and we are moving them respectively above.

Making A Service User

Now this is an optional step, it is not a best practice to run everything with a root user. So we need to create a user for Nexus and run this program with that user. We need to give this user the necessary file permissions.

In order to create a User run :

sudo useradd -s /bin/bash nexus
sudo passwd nexus

The first command will create a nexus user for you and then by running the second command it will ask you to set a new password for that user.

chown -R nexus:nexus /opt/nexus /opt/sonatype-work

The above command will set permission for newly created directories to nexus users.

Now we have to tell Nexus with which user we need to run this Nexus service default is set to root.

Open nexus.rc with nano editor or any other editor of your choice.

sudo nano /opt/nexus/bin/nexus.rc
run_as_user="nexus"

uncomment the above option and write nexus in front of it like above.

You can also set the maxHeap setting but it's not that important as a beginner.

Now finally run the below command to run Nexus Service.

/opt/nexus/nexus-3.41.101/bin/nexus start

You can now access your Nexus UI on Port 8081.

Let’s Run Nexus On Container

This is the process I use to run Nexus, I don’t prefer to install everything on my system, so let us quickly check how we can do that.

Now running Nexus on Docker is very easy it is a 2 command process believe me, you just need to make sure that Docker is available on the system where you want to run Nexus. Then you run the following command for Docker to run a Nexus container.

docker volume create --name nexus-data

The above command will create a volume where Nexus will save all of its data.

docker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3

This command will spin up a container using that volume, here in this command we are defining port 8081:8081 so we can access it on our machine.

Bingo! your container is running you can now go to PORT 8081 to access the Nexus UI.

Conclusion

In this first part of our Nexus blog series, we’ve explored the basics of Nexus, compared it with other artifact managers, and provided a detailed guide on installing Nexus on Linux and running it on Docker containers.

If you found this content helpful, please show your support by giving it a clap, following me on LinkedIn and YouTube, and considering making a small contribution to Buy Me a Coffee☕️. Your support is greatly appreciated!

Did you find this article valuable?

Support Muhammad Younus by becoming a sponsor. Any amount is appreciated!